import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    name: 'index',
    meta: { title: '首页', icon: 'el-icon-s-home' },
    children: [
      {
        path: 'index',
        name: 'welcome',
        component: () => import('@/views/index/index.vue'),
        meta: { title: '首页', icon: 'el-icon-s-home' }
      }
    ]
  },{
    path: '/adx',
    component: Layout,
    redirect: '/adx/adxlist',
    name: 'adx-list',
    meta: { title: '媒体管理', icon: 'el-icon-s-shop' },
    children: [
      {
        path: 'adxlist',
        name: 'adx-list',
        component: () => import('@/views/adxlist/list.vue'),
        meta: { title: 'ADX列表', icon: 'el-icon-reading' },
      },
      {
        path: 'media',
        name: 'media-list',
        component: () => import('@/views/adxapp/list.vue'),
        meta: { title: '媒体列表', icon: 'el-icon-data-analysis' },
      },
      {
        path: 'slot',
        name: 'slot-list',
        component: () => import('@/views/adxslot/list.vue'),
        meta: { title: '广告位置', icon: 'el-icon-data-board' },
      },
      {
        path: 'adxtemplate',
        name: 'adxtemplate-list',
        component: () => import('@/views/adxtemplate/list.vue'),
        meta: { title: '媒体模版', icon: 'el-icon-s-shop' },
      },
    ]
  },{
    path: '/dsp',
    component: Layout,
    redirect: '/dsp/dsplist',
    name: 'dsplist-list',
    meta: { title: '预算管理', icon: 'el-icon-s-finance' },
    children: [
      {
        path: 'dsplist',
        name: 'dsplist-list',
        component: () => import('@/views/channellist/list.vue'),
        meta: { title: '预算列表', icon: 'el-icon-suitcase-1' },
      },
      {
        path: 'slot',
        name: 'dspslot-list',
        component: () => import('@/views/channelslot/list.vue'),
        meta: { title: '预算位置', icon: 'el-icon-suitcase' },
      },
      {
        path: 'dsptemplate',
        name: 'dsptemplate-list',
        component: () => import('@/views/channeltemplate/list.vue'),
        meta: { title: '预算模版', icon: 'el-icon-files' },
      },
    ]
  },{
    path: '/ssp',
    component: Layout,
    redirect: '/ssp/ssplist',
    name: 'ssplist-list',
    meta: { title: 'SSP管理', icon: 'el-icon-s-platform' },
    children: [
      {
        path: 'ssplist',
        name: 'ssplist-list',
        component: () => import('@/views/ssplist/list.vue'),
        meta: { title: 'SSP列表', icon: 'el-icon-box' },
      },
      {
        path: 'app',
        name: 'sspapp-list',
        component: () => import('@/views/sspapp/list.vue'),
        meta: { title: 'SSP应用', icon: 'el-icon-bank-card' },
      },
      {
        path: 'slot',
        name: 'sspslot-list',
        component: () => import('@/views/sspslot/list.vue'),
        meta: { title: 'SSP位置', icon: 'el-icon-money' },
      },
      {
        path: 'ssptemplate',
        name: 'ssptemplate-list',
        component: () => import('@/views/ssptemplate/list.vue'),
        meta: { title: 'SSP模版', icon: 'el-icon-s-shop' },
      },
    ]
  },{
    path: '/plan',
    component: Layout,
    redirect: '/plan/planlist',
    name: 'planlist-list',
    meta: { title: '计划管理', icon: 'el-icon-s-ticket' },
    children: [
      {
        path: 'planlist',
        name: 'planlist-list',
        component: () => import('@/views/adxplan/list.vue'),
        meta: { title: '计划列表', icon: 'el-icon-s-ticket' },
      },
    ]
  },
  {
    path: '/report',
    component: Layout,
    redirect: '/report/index',
    name: 'report',
    meta: { title: '数据报表', icon: 'el-icon-s-data' },
    children: [
      {
        path: 'index',
        name: 'report-media',
        component: () => import('@/views/report/adx_report.vue'),
        meta: { title: '流量报表', icon: 'el-icon-s-data' },
      },
      {
        path: 'adx-channel-report',
        name: 'report-adx-channel',
        component: () => import('@/views/report/adx_channel_report.vue'),
        meta: { title: '预算报表', icon: 'el-icon-s-data' },
      },
      {
        path: 'ssp-report',
        name: 'report-ssp',
        component: () => import('@/views/report/ssp_report.vue'),
        meta: { title: 'SSP报表', icon: 'el-icon-s-data' },
      },
      {
        path: 'ssp-channel-report',
        name: 'report-ssp-channel',
        component: () => import('@/views/report/ssp_channel_report.vue'),
        meta: { title: 'SSP收益', icon: 'el-icon-s-data' },
      }
    ]
  }
]

export const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
